package com.mmy.fta.usercenter.service.impl;

import com.mmy.fta.usercenter.api.MembershipService;
import com.mmy.fta.usercenter.dto.CouponDTO;
import com.mmy.fta.usercenter.entity.CouponEntity;
import com.mmy.fta.usercenter.entity.DriverMembershipEntity;
import com.mmy.fta.usercenter.entity.OpenidUserEntity;
import com.mmy.fta.usercenter.entity.ShipperMembershipEntity;
import com.mmy.fta.usercenter.enums.DriverRenewEnum;
import com.mmy.fta.usercenter.enums.DriverMemberShipTypeEnums;
import com.mmy.fta.usercenter.enums.ShipperMemberShipTypeEnums;
import com.mmy.fta.usercenter.enums.ShipperRenewEnum;
import com.mmy.fta.usercenter.repository.*;
import com.mmy.fta.usercenter.request.DriverMemberShipRequest;
import com.mmy.fta.usercenter.request.DriverRenewRequest;
import com.mmy.fta.usercenter.request.ShipperMemberShipRequest;
import com.mmy.fta.usercenter.response.DriverImmediateRenewResponse;
import com.mmy.fta.usercenter.response.MembershipEndTimeResponse;
import com.mmy.fta.usercenter.response.MyMsgResponse;
import com.mmy.fta.usercenter.response.ShipperImmediateRenewResponse;
import com.ymm.common.rpc.YmmResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author qingyu.meng
 * @date 2021年10月29日 10:50 上午
 * @Description
 */

@Service
@Slf4j
public class MembershipServiceImpl implements MembershipService {
    @Autowired
    DriverMembershipEntityMapper driverMembershipEntityMapper;
    @Autowired
    ShipperMembershipEntityMapper shipperMembershipEntityMapper;
    @Autowired
    OpenidUserMapper openidUserMapper;
    @Autowired
    ShipperInfoEntityMapper shipperInfoEntityMapper;
    @Autowired
    DriverInfoEntityMapper driverInfoEntityMapper;
    @Autowired
    CouponEntityMapper couponEntityMapper;

    /**
     * 查询尊享会员到期时间
     */
    @Override
    public YmmResult membershipEndTime(String openId) {
        log.info(">>>>>>MembershipService:membershipEndTime,openId is {}<<<<<<", openId);
        MembershipEndTimeResponse membershipEndTimeResponse = new MembershipEndTimeResponse();
        MyMsgResponse myMsgResponse = new MyMsgResponse();
        OpenidUserEntity user = openidUserMapper.selectByOpenId(openId);
        Integer appType = user.getAppType();
        DriverMembershipEntity driverMembershipEntity;
        ShipperMembershipEntity shipperMembershipEntity;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        int freetimeopenidUser = openidUserMapper.selectFreetime(openId);
        int coupons = openidUserMapper.selectCoupons(openId);

        if (appType == 0) {//司机端
            driverMembershipEntity = driverMembershipEntityMapper.selectByOpenId(openId);
            if (Objects.isNull(driverMembershipEntity) || Objects.isNull(driverMembershipEntity.getNormalEndTime())) {
                membershipEndTimeResponse.setFreeTime(freetimeopenidUser);
                membershipEndTimeResponse.setCoupons(coupons);
                log.info("司机非会员、免费发货次数、优惠券张数：{}", membershipEndTimeResponse);
                return YmmResult.succResult(membershipEndTimeResponse);
            }
            int totalFreetime = freetimeopenidUser;
            String endTime = simpleDateFormat.format(driverMembershipEntity.getNormalEndTime());
            membershipEndTimeResponse.setEndTime(endTime.substring(0, 10));
            membershipEndTimeResponse.setFreeTime(totalFreetime);
            membershipEndTimeResponse.setCoupons(coupons);
            log.info("司机会员到期时间、免费发货次数、优惠券张数：{}", membershipEndTimeResponse);
            return YmmResult.succResult(membershipEndTimeResponse);
        } else {//货主端
            shipperMembershipEntity = shipperMembershipEntityMapper.selectByOpenId(openId);
            if (Objects.isNull(shipperMembershipEntity) || Objects.isNull(shipperMembershipEntity.getNormalEndTime())) {
                membershipEndTimeResponse.setFreeTime(freetimeopenidUser);
                membershipEndTimeResponse.setCoupons(coupons);
                log.info("货主非会员、免费发货次数、优惠券张数：{}", membershipEndTimeResponse);
                return YmmResult.succResult(membershipEndTimeResponse);
            }
            int totalFreetime = freetimeopenidUser;
            String endTime = simpleDateFormat.format(shipperMembershipEntity.getNormalEndTime());
            membershipEndTimeResponse.setEndTime(endTime.substring(0, 10));
            membershipEndTimeResponse.setFreeTime(totalFreetime);
            membershipEndTimeResponse.setCoupons(coupons);
            log.info("货主会员到期时间、免费发货次数、优惠券张数：{}", membershipEndTimeResponse);
            return YmmResult.succResult(membershipEndTimeResponse);
        }

    }

    @Override
    public YmmResult<Date> openMembership(String openId) {
        log.info(">>>>>>MembershipService:openMembership,openId is {}<<<<<<", openId);
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        Date current = calendar.getTime();
        calendar.add(Calendar.MONTH, 3);
        Date threeMonthsLater = calendar.getTime();

        DriverMembershipEntity driverMembershipEntity = new DriverMembershipEntity();
        driverMembershipEntity.setNormalBeginTime(current);
        // 季度会员
        driverMembershipEntity.setNormalEndTime(threeMonthsLater);
        driverMembershipEntity.setOpenId(openId);
        driverMembershipEntity.setFreeTimes(10L);

        int result = driverMembershipEntityMapper.insertSelective(driverMembershipEntity);
        if (result == 0) {
            return YmmResult.failResult(000, "开通会员失败");
        }

        return YmmResult.succResult(threeMonthsLater);
    }

    /**
     * 扣减司机会员免技术服务费次数
     *
     * @param openId
     * @return
     */
    @Override
    public YmmResult useFreeTime(String openId) {
        log.info(">>>>>>MemberShipService:useFreeTime,openId is {}<<<<<<", openId);


//        if (Objects.isNull(driverMembershipEntity)) {
//            return YmmResult.failResult(000, "该openId对应的会员不存在");
//        }

        OpenidUserEntity openidUserEntity = openidUserMapper.selectByOpenId(openId);

        Integer freeTimes = openidUserEntity.getIsValid();
        freeTimes--;

        int isNormalMember = openidUserMapper.selectDriverIsNormalMembership(openId);

        if (isNormalMember == 1) {
            int res = driverMembershipEntityMapper.descDriverMemberFreeTime(openId);
            if (res == 1) {
                log.info("driverMembership-司机会员-freetimes-1");
            }
        }

        int res2 = openidUserMapper.decDriverFreeTimesByOpenId(openId);

        if (res2 == 1) {
            log.info("openid_user-司机-freetimes-1");
        }
        return YmmResult.succResult();
    }

    @Override
    public YmmResult continueMembership(String openId) {

        return null;
    }

    /**
     * 司机查询会员接口
     */
    @Override
    public YmmResult<DriverImmediateRenewResponse> driverImmediateRenew(String openId) {
        int freetimeopenidUser = openidUserMapper.selectFreetime(openId);
        DriverImmediateRenewResponse driverImmediateRenewResponse = new DriverImmediateRenewResponse();
        DriverMembershipEntity driverMembershipEntity = driverMembershipEntityMapper.selectByOpenId(openId);
        if (Objects.isNull(driverMembershipEntity)) {
            //非会员
            driverImmediateRenewResponse.setFreeTimes((long) freetimeopenidUser);
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.ZERO.getMsg());
            return YmmResult.succResult(driverImmediateRenewResponse);
        }

        driverMembershipEntity.setFreeTimes((long) freetimeopenidUser);
        BeanUtils.copyProperties(driverMembershipEntity, driverImmediateRenewResponse);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //保油会员开始时间
        if (Objects.nonNull(driverMembershipEntity.getOilBeginTime())) {
            String oilBeginTime = simpleDateFormat.format(driverMembershipEntity.getOilBeginTime());
            driverImmediateRenewResponse.setOilBeginTime(oilBeginTime.substring(0, 10));
        }
        //保油会员结束时间
        if (Objects.nonNull(driverMembershipEntity.getOilEndTime())) {
            String oilEndTime = simpleDateFormat.format(driverMembershipEntity.getOilEndTime());
            driverImmediateRenewResponse.setOilEndTime(oilEndTime.substring(0, 10));
        }
        //冷链会员开始时间
        if (Objects.nonNull(driverMembershipEntity.getColdBeginTime())) {
            String coldBeginTime = simpleDateFormat.format(driverMembershipEntity.getColdBeginTime());
            driverImmediateRenewResponse.setColdBeginTime(coldBeginTime.substring(0, 10));
        }
        //冷链会员结束时间
        if (Objects.nonNull(driverMembershipEntity.getColdEndTime())) {
            String coldEndTime = simpleDateFormat.format(driverMembershipEntity.getColdEndTime());
            driverImmediateRenewResponse.setColdEndTime(coldEndTime.substring(0, 10));
        }
        //尊享会员开始时间
        if (Objects.nonNull(driverMembershipEntity.getNormalBeginTime())) {
            String normalBeginTime = simpleDateFormat.format(driverMembershipEntity.getNormalBeginTime());
            driverImmediateRenewResponse.setNormalBeginTime(normalBeginTime.substring(0, 10));
        }
        //尊享会员结束时间
        if (Objects.nonNull(driverMembershipEntity.getNormalEndTime())) {
            String normalEndTime = simpleDateFormat.format(driverMembershipEntity.getNormalEndTime());
            driverImmediateRenewResponse.setNormalEndTime(normalEndTime.substring(0, 10));
        }
        //会员类型
        if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.ZERO.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.ZERO.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.FIRST.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.FIRST.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.SECOND.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.SECOND.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.THIRD.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.THIRD.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.FOUTH.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.FOUTH.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.FIFTH.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.FIFTH.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.SIXTH.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.SIXTH.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverMemberShipTypeEnums.SEVENTH.getCode())) {
            driverImmediateRenewResponse.setMembershipType(DriverMemberShipTypeEnums.SEVENTH.getMsg());
        }
        int coupons10 = couponEntityMapper.selectCoupons10(openId);
        int coupons20 = couponEntityMapper.selectCoupons20(openId);
        int coupons30 = couponEntityMapper.selectCoupons30(openId);
        driverImmediateRenewResponse.setMonthMileage(driverMembershipEntity.getMonthMileage());
        CouponEntity couponEntity = new CouponEntity();
        if (1200 <= driverMembershipEntity.getMonthMileage()
                && driverMembershipEntity.getMonthMileage() < 1800) {
            if (Objects.equals(coupons10, 0)) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(10d);
                couponEntity.setCouponType(1);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
        } else if (1800 <= driverMembershipEntity.getMonthMileage()
                && driverMembershipEntity.getMonthMileage() < 2400) {
            if (Objects.equals(coupons10, 0)) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(10d);
                couponEntity.setCouponType(1);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
            if (Objects.equals(coupons20, 0)) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(20d);
                couponEntity.setCouponType(1);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
        } else if (driverMembershipEntity.getMonthMileage() >= 2400) {
            if (Objects.equals(coupons10, 0)) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(10d);
                couponEntity.setCouponType(1);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
            if (Objects.equals(coupons20, 0)) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(20d);
                couponEntity.setCouponType(1);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
            if (Objects.equals(coupons30, 0)) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(30d);
                couponEntity.setCouponType(1);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
        }
        List<CouponEntity> couponsCouponEntity = couponEntityMapper.selectCoupons(openId);
        List<CouponDTO> couponsCouponDTO = new ArrayList<>(couponsCouponEntity.size());
        for (CouponEntity cou : couponsCouponEntity) {
            CouponDTO couponDTO = new CouponDTO();
            couponDTO.setCouponId(cou.getCouponId());
            couponDTO.setCouponAmount(cou.getCouponAmount());
            couponDTO.setCouponType(cou.getCouponType());
            couponDTO.setBeginTime(cou.getBeginTime());
            couponDTO.setEndTime(cou.getEndTime());
            couponDTO.setIsValid(cou.getIsValid());
            couponDTO.setLimitApptype(cou.getLimitApptype());
            log.info("couponsCouponDTO:{}", couponsCouponDTO.size());
            couponsCouponDTO.add(couponDTO);
        }
        if (Objects.equals(couponsCouponDTO.size(), 0)) {
            CouponDTO couponDTO = new CouponDTO();
            int remain1 = 1200 - driverMembershipEntity.getMonthMileage();
            couponDTO.setIsValid(0);
            couponDTO.setCouponId(String.valueOf(remain1));
            couponsCouponDTO.add(couponDTO);
            CouponDTO couponDTO1 = new CouponDTO();
            int remain2 = 1800 - driverMembershipEntity.getMonthMileage();
            couponDTO1.setIsValid(0);
            couponDTO1.setCouponId(String.valueOf(remain2));
            couponsCouponDTO.add(couponDTO1);
            CouponDTO couponDTO2 = new CouponDTO();
            int remain3 = 2400 - driverMembershipEntity.getMonthMileage();
            couponDTO2.setIsValid(0);
            couponDTO2.setCouponId(String.valueOf(remain3));
            couponsCouponDTO.add(couponDTO2);
            driverImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(driverImmediateRenewResponse);
        } else if (Objects.equals(couponsCouponDTO.size(), 1)) {
            CouponDTO couponDTO = new CouponDTO();
            int remain2 = 1800 - driverMembershipEntity.getMonthMileage();
            couponDTO.setIsValid(0);
            couponDTO.setCouponId(String.valueOf(remain2));
            couponsCouponDTO.add(couponDTO);
            CouponDTO couponDTO1 = new CouponDTO();
            int remain3 = 2400 - driverMembershipEntity.getMonthMileage();
            couponDTO1.setIsValid(0);
            couponDTO1.setCouponId(String.valueOf(remain3));
            couponsCouponDTO.add(couponDTO1);
            driverImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(driverImmediateRenewResponse);
        } else if (Objects.equals(couponsCouponDTO.size(), 2)) {
            CouponDTO couponDTO = new CouponDTO();
            int remain3 = 2400 - driverMembershipEntity.getMonthMileage();
            couponDTO.setIsValid(0);
            couponDTO.setCouponId(String.valueOf(remain3));
            couponsCouponDTO.add(couponDTO);
            driverImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(driverImmediateRenewResponse);
        } else if (Objects.equals(couponsCouponDTO.size(), 3)) {
            driverImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(driverImmediateRenewResponse);
        }
        log.info("返券失败");
        return YmmResult.succResult(driverImmediateRenewResponse);
    }

    public static Date nextMonthFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    public static Date nextnextMonthFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 2);
        return calendar.getTime();
    }

    /**
     * 货主查询会员接口
     */
    @Override
    public YmmResult<ShipperImmediateRenewResponse> shipperImmediateRenew(String openId) {
        int freetimeopenidUser = openidUserMapper.selectFreetime(openId);
        ShipperImmediateRenewResponse shipperImmediateRenewResponse = new ShipperImmediateRenewResponse();
        ShipperMembershipEntity shipperMembershipEntity = shipperMembershipEntityMapper.selectByOpenId(openId);
        if (Objects.isNull(shipperMembershipEntity)) {
            //非会员
            shipperImmediateRenewResponse.setSendCargoTimes((long) freetimeopenidUser);
            shipperImmediateRenewResponse.setMembershipType(ShipperMemberShipTypeEnums.ZERO.getMsg());
            return YmmResult.succResult(shipperImmediateRenewResponse);
        }
        shipperMembershipEntity.setSendCargoTimes((long) freetimeopenidUser);
        BeanUtils.copyProperties(shipperMembershipEntity, shipperImmediateRenewResponse);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (Objects.nonNull(shipperMembershipEntity.getColdBeginTime())) {
            String coldBeginTime = simpleDateFormat.format(shipperMembershipEntity.getColdBeginTime());
            shipperImmediateRenewResponse.setColdBeginTime(coldBeginTime.substring(0, 10));
        }
        if (Objects.nonNull(shipperMembershipEntity.getColdEndTime())) {
            String coldEndTime = simpleDateFormat.format(shipperMembershipEntity.getColdEndTime());
            shipperImmediateRenewResponse.setColdEndTime(coldEndTime.substring(0, 10));
        }
        if (Objects.nonNull(shipperMembershipEntity.getNormalBeginTime())) {
            String normalBeginTime = simpleDateFormat.format(shipperMembershipEntity.getNormalBeginTime());
            shipperImmediateRenewResponse.setNormalBeginTime(normalBeginTime.substring(0, 10));
        }
        if (Objects.nonNull(shipperMembershipEntity.getNormalEndTime())) {
            String normalEndTime = simpleDateFormat.format(shipperMembershipEntity.getNormalEndTime());
            shipperImmediateRenewResponse.setNormalEndTime(normalEndTime.substring(0, 10));
        }
        if (Objects.equals(shipperMembershipEntity.getMembershipType(), ShipperMemberShipTypeEnums.ZERO.getCode())) {
            shipperImmediateRenewResponse.setMembershipType(ShipperMemberShipTypeEnums.ZERO.getMsg());
        } else if (Objects.equals(shipperMembershipEntity.getMembershipType(), ShipperMemberShipTypeEnums.FIRST.getCode())) {
            shipperImmediateRenewResponse.setMembershipType(ShipperMemberShipTypeEnums.FIRST.getMsg());
        } else if (Objects.equals(shipperMembershipEntity.getMembershipType(), ShipperMemberShipTypeEnums.SECOND.getCode())) {
            shipperImmediateRenewResponse.setMembershipType(ShipperMemberShipTypeEnums.SECOND.getMsg());
        } else if (Objects.equals(shipperMembershipEntity.getMembershipType(), ShipperMemberShipTypeEnums.THIRD.getCode())) {
            shipperImmediateRenewResponse.setMembershipType(ShipperMemberShipTypeEnums.THIRD.getMsg());
        }

        int coupons95 = couponEntityMapper.selectCoupons95(openId);
        int coupons85 = couponEntityMapper.selectCoupons85(openId);
        CouponEntity couponEntity = new CouponEntity();
        double rate = new Float(shipperMembershipEntity.getMonthDeals()) / new Float(shipperMembershipEntity.getMonthDeliverys());
        shipperImmediateRenewResponse.setMonthDeliverys(shipperMembershipEntity.getMonthDeliverys());
        shipperImmediateRenewResponse.setMonthRate((double) Math.round(rate * 100));


        if (0.4 <= rate && rate < 0.6) {
            if (Objects.equals(coupons95, 0) && shipperMembershipEntity.getMonthDeliverys() >= 5) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(95d);
                couponEntity.setCouponType(2);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
        } else if (rate >= 0.6) {
            if (Objects.equals(coupons95, 0) && shipperMembershipEntity.getMonthDeliverys() >= 5) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(95d);
                couponEntity.setCouponType(2);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
            if (Objects.equals(coupons85, 0) && shipperMembershipEntity.getMonthDeliverys() >= 10) {
                couponEntity.setOpenId(openId);
                couponEntity.setCouponAmount(85d);
                couponEntity.setCouponType(2);
                Date nextMonthFirstDate = nextMonthFirstDate();
                couponEntity.setBeginTime(nextMonthFirstDate);
                Date nextnextMonthFirstDate = nextnextMonthFirstDate();
                couponEntity.setEndTime(nextnextMonthFirstDate);
                couponEntity.setIsValid(1);
                couponEntityMapper.insertSelective(couponEntity);
            }
        }

        List<CouponEntity> couponsCouponEntity = couponEntityMapper.selectCoupons(openId);
        List<CouponDTO> couponsCouponDTO = new ArrayList<>(couponsCouponEntity.size());
        for (int i = 0; i < couponsCouponEntity.size(); i++) {
            CouponDTO couponDTO = new CouponDTO();
            if (i == 0) {
                if (shipperMembershipEntity.getMonthDeliverys() >= 5 && rate >= 0.4) {
                    couponDTO.setCouponId(couponsCouponEntity.get(i).getCouponId());
                    couponDTO.setCouponAmount(couponsCouponEntity.get(i).getCouponAmount());
                    couponDTO.setCouponType(couponsCouponEntity.get(i).getCouponType());
                    couponDTO.setBeginTime(couponsCouponEntity.get(i).getBeginTime());
                    couponDTO.setEndTime(couponsCouponEntity.get(i).getEndTime());
                    couponDTO.setIsValid(couponsCouponEntity.get(i).getIsValid());
                } else {
                    //得95折券的要求：发货至少5次未达成，不给券
                    couponDTO.setCouponId("未达标");
                }
            }
            if (i == 1) {
                if (shipperMembershipEntity.getMonthDeliverys() >= 10 && rate >= 0.6) {
                    couponDTO.setCouponId(couponsCouponEntity.get(i).getCouponId());
                    couponDTO.setCouponAmount(couponsCouponEntity.get(i).getCouponAmount());
                    couponDTO.setCouponType(couponsCouponEntity.get(i).getCouponType());
                    couponDTO.setBeginTime(couponsCouponEntity.get(i).getBeginTime());
                    couponDTO.setEndTime(couponsCouponEntity.get(i).getEndTime());
                    couponDTO.setIsValid(couponsCouponEntity.get(i).getIsValid());
                } else {
                    //得85折券的要求：发货至少10次未达成，不给券
                    couponDTO.setCouponId("未达标");
                }
            }

            couponDTO.setLimitApptype(couponsCouponEntity.get(i).getLimitApptype());
            log.info("couponsCouponDTO:{}", couponsCouponDTO.size());
            couponsCouponDTO.add(couponDTO);
        }
        if (Objects.equals(couponsCouponDTO.size(), 0)) {
            CouponDTO couponDTO = new CouponDTO();
            int rate1 = (int) Math.ceil(shipperMembershipEntity.getMonthDeliverys() * 0.4) - shipperMembershipEntity.getMonthDeals();
            couponDTO.setCouponId(String.valueOf(rate1));
            couponDTO.setIsValid(0);
            couponsCouponDTO.add(couponDTO);
            CouponDTO couponDTO1 = new CouponDTO();
            int rate2 = (int) Math.ceil(shipperMembershipEntity.getMonthDeliverys() * 0.6) - shipperMembershipEntity.getMonthDeals();
            couponDTO1.setCouponId(String.valueOf(rate2));
            couponsCouponDTO.add(couponDTO1);
            shipperImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(shipperImmediateRenewResponse);
        } else if (Objects.equals(couponsCouponDTO.size(), 1)) {
            CouponDTO couponDTO = new CouponDTO();
            int rate2 = (int) Math.ceil(shipperMembershipEntity.getMonthDeliverys() * 0.6) - shipperMembershipEntity.getMonthDeals();
            couponDTO.setCouponId(String.valueOf(rate2));
            couponDTO.setIsValid(0);
            couponsCouponDTO.add(couponDTO);
            shipperImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(shipperImmediateRenewResponse);
        } else if (Objects.equals(couponsCouponDTO.size(), 2)) {
            shipperImmediateRenewResponse.setCoupons(couponsCouponDTO);
            return YmmResult.succResult(shipperImmediateRenewResponse);
        }


        log.info("返券失败");
        return YmmResult.succResult(shipperImmediateRenewResponse);

    }


    /**
     * 货主开通/续费会员
     */
    @Override
    public YmmResult shipperRenew(ShipperMemberShipRequest shipperMemberShipRequest) {
        int shipperMemberShipStatus = openidUserMapper.selectShipperMemberShipStatus(shipperMemberShipRequest.getOpenId());
        String type = "";
        if (shipperMemberShipRequest.getRenew() == 0) {
            //开通
            if (Objects.equals(shipperMemberShipRequest.getStatus(), ShipperRenewEnum.NORMAL.getCode())) {
                ShipperMembershipEntity shipperMembershipEntity = new ShipperMembershipEntity();

                ShipperMembershipEntity s1 = shipperMembershipEntityMapper.selectShipperMemberByOpenId(shipperMemberShipRequest.getOpenId());

                if (Objects.nonNull(s1)) {
                    shipperMembershipEntity = s1;
                }

                shipperMembershipEntity.setMembershipType(2);
                Date beginTime = new Date();
                Date endTime = stepMonth(beginTime, 3);
                shipperMembershipEntity.setNormalBeginTime(beginTime);
                shipperMembershipEntity.setNormalEndTime(endTime);
                shipperMembershipEntity.setCreateTime(beginTime);
                shipperMembershipEntity.setUpdateTime(beginTime);
                type = "normal";
                int result = openShipperMemberShip(shipperMemberShipRequest, shipperMemberShipStatus, shipperMembershipEntity, type);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            } else if (Objects.equals(shipperMemberShipRequest.getStatus(), ShipperRenewEnum.COLD.getCode())) {

                ShipperMembershipEntity shipperMembershipEntity = new ShipperMembershipEntity();

                ShipperMembershipEntity s1 = shipperMembershipEntityMapper.selectShipperMemberByOpenId(shipperMemberShipRequest.getOpenId());

                if (Objects.nonNull(s1)) {
                    shipperMembershipEntity = s1;
                }

                shipperMembershipEntity.setMembershipType(1);
                Date beginTime = new Date();
                Date endTime = stepMonth(beginTime, 3);
                shipperMembershipEntity.setColdBeginTime(beginTime);
                shipperMembershipEntity.setColdEndTime(endTime);
                shipperMembershipEntity.setCreateTime(beginTime);
                shipperMembershipEntity.setUpdateTime(beginTime);
                type = "cold";
                int result = openShipperMemberShip(shipperMemberShipRequest, shipperMemberShipStatus, shipperMembershipEntity, type);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            }
        } else {
            //续费
            if (Objects.equals(shipperMemberShipRequest.getStatus(), ShipperRenewEnum.NORMAL.getCode())) {
                int result = shipperMembershipEntityMapper.updateNormalMemberShipTime(shipperMemberShipRequest.getOpenId());
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            } else if (Objects.equals(shipperMemberShipRequest.getStatus(), ShipperRenewEnum.COLD.getCode())) {
                int result = shipperMembershipEntityMapper.updateColdMemberShipTime(shipperMemberShipRequest.getOpenId());
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            }
        }
        return YmmResult.succResult(0);

    }

    @Override
    public boolean driverIsColdMembership(String driverOpenId) {
        int driverIsColdMembership = openidUserMapper.selectDriverIsColdMembership(driverOpenId);
        return driverIsColdMembership == 1;
    }

    @Override
    public void addShipperMembershipMonthDeals(String shipperOpenId) {
        int shipperIsMember = openidUserMapper.selectShipperIsNormalMembership(shipperOpenId);
        if (shipperIsMember == 0) {
            //非会员
            return;
        } else {
            int res = shipperMembershipEntityMapper.addShipperMemberMonthDeals(shipperOpenId);
        }

    }

    @Override
    public void addDriverMembershipMonthMileage(String driverOpenId, Integer mileage) {
        int driverIsMember = openidUserMapper.selectDriverIsNormalMembership(driverOpenId);
        if (driverIsMember == 0) {
            //非会员
            return;
        } else {
            int res = driverMembershipEntityMapper.addDriverMembershipMonthMileage(driverOpenId, mileage);
            return;
        }

    }


    /**
     * 司机开通/续费会员
     */
    @Override
    public YmmResult driverRenew(DriverMemberShipRequest driverMemberShipRequest) {
        int driverMemberShipStatus = openidUserMapper.selectDriverMemberShipStatus(driverMemberShipRequest.getOpenId());
        String type = "";
        if (driverMemberShipRequest.getRenew() == 0) {
            //开通
            if (Objects.equals(driverMemberShipRequest.getStatus(), DriverRenewEnum.NORMAL.getCode())) {
                DriverMembershipEntity driverMembershipEntity = new DriverMembershipEntity();

                DriverMembershipEntity d1 = driverMembershipEntityMapper.selectDriverMemberByOpenId(driverMemberShipRequest.getOpenId());
                if(Objects.nonNull(d1)){
                    driverMembershipEntity = d1;
                }
                driverMembershipEntity.setMembershipType(4);
                Date beginTime = new Date();
                Date endTime = stepMonth(beginTime, 3);
                driverMembershipEntity.setNormalBeginTime(beginTime);
                driverMembershipEntity.setNormalEndTime(endTime);
                driverMembershipEntity.setCreateTime(beginTime);
                driverMembershipEntity.setUpdateTime(beginTime);
                type = "normal";
                int result = openDriverMemberShip(driverMemberShipRequest, driverMemberShipStatus, driverMembershipEntity, type);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            } else if (Objects.equals(driverMemberShipRequest.getStatus(), DriverRenewEnum.OIL.getCode())) {
                DriverMembershipEntity driverMembershipEntity = new DriverMembershipEntity();

                DriverMembershipEntity d1 = driverMembershipEntityMapper.selectDriverMemberByOpenId(driverMemberShipRequest.getOpenId());
                if(Objects.nonNull(d1)){
                    driverMembershipEntity = d1;
                }
                driverMembershipEntity.setMembershipType(2);
                Date beginTime = new Date();
                Date endTime = stepMonth(beginTime, 3);
                driverMembershipEntity.setOilBeginTime(beginTime);
                driverMembershipEntity.setOilEndTime(endTime);
                driverMembershipEntity.setCreateTime(beginTime);
                driverMembershipEntity.setUpdateTime(beginTime);
                type = "oil";
                int result = openDriverMemberShip(driverMemberShipRequest, driverMemberShipStatus, driverMembershipEntity, type);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            } else if (Objects.equals(driverMemberShipRequest.getStatus(), DriverRenewEnum.COLD.getCode())) {
                DriverMembershipEntity driverMembershipEntity = new DriverMembershipEntity();

                DriverMembershipEntity d1 = driverMembershipEntityMapper.selectDriverMemberByOpenId(driverMemberShipRequest.getOpenId());
                if(Objects.nonNull(d1)){
                    driverMembershipEntity = d1;
                }
                driverMembershipEntity.setMembershipType(1);
                Date beginTime = new Date();
                Date endTime = stepMonth(beginTime, 3);
                driverMembershipEntity.setColdBeginTime(beginTime);
                driverMembershipEntity.setColdEndTime(endTime);
                driverMembershipEntity.setCreateTime(beginTime);
                driverMembershipEntity.setUpdateTime(beginTime);
                type = "cold";
                int result = openDriverMemberShip(driverMemberShipRequest, driverMemberShipStatus, driverMembershipEntity, type);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            }
        } else {
            //续费
            if (Objects.equals(driverMemberShipRequest.getStatus(), DriverRenewEnum.NORMAL.getCode())) {
                int result = driverMembershipEntityMapper.updateNormalMemberShipTime(driverMemberShipRequest.getOpenId());
                log.info("司机续费尊享会员：{}", result);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            } else if (Objects.equals(driverMemberShipRequest.getStatus(), DriverRenewEnum.OIL.getCode())) {
                int result = driverMembershipEntityMapper.updateOilMemberShipTime(driverMemberShipRequest.getOpenId());
                log.info("司机续油保享会员：{}", result);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            } else if (Objects.equals(driverMemberShipRequest.getStatus(), DriverRenewEnum.COLD.getCode())) {
                int result = driverMembershipEntityMapper.updateColdMemberShipTime(driverMemberShipRequest.getOpenId());
                log.info("司机续冷链享会员：{}", result);
                if (Objects.equals(result, 0)) {
                    return YmmResult.succResult(0);
                }
                return YmmResult.succResult(1);
            }
        }
        return YmmResult.succResult(0);

    }

    //司机开通会员
    private int openDriverMemberShip(DriverMemberShipRequest driverMemberShipRequest, int driverMemberShipStatus, DriverMembershipEntity driverMembershipEntity, String type) {
//        long freeTime = 10;
        driverMembershipEntity.setOpenId(driverMemberShipRequest.getOpenId());
//        driverMembershipEntity.setFreeTimes(freeTime);
        if (Objects.equals(driverMemberShipStatus, 0)) {
            //首次开通(之前未开通任何会员，数据库无记录)，插入
            int result = driverMembershipEntityMapper.insertNewDriverMember(driverMembershipEntity);
            //首次开会员，写库标记“会员”
            int res = openidUserMapper.updateMemberShipStatus(driverMembershipEntity.getOpenId());
            if (Objects.equals(type, "normal")) {
                //赠送司机权益
                driverGift(driverMembershipEntity);
            } else if (Objects.equals(type, "oil")) {
                int resOilMemberShipStatus = openidUserMapper.updateOilMemberShipStatus(driverMembershipEntity.getOpenId());
            } else {
                int resColdMemberShipStatus = openidUserMapper.updateColdMemberShipStatus(driverMembershipEntity.getOpenId());
            }
            log.info("司机开通会员(之前不是任何会员)，插入影响行数：{}", result);
            return result;
        } else {
            //非首次开通(之前开过某个会员，有记录)，更新即可
            if (Objects.equals(type, "normal")) {
                //赠送司机权益
                driverGift(driverMembershipEntity);
            } else if (Objects.equals(type, "oil")) {
                int resOilMemberShipStatus = openidUserMapper.updateOilMemberShipStatus(driverMembershipEntity.getOpenId());
            } else {
                int resColdMemberShipStatus = openidUserMapper.updateColdMemberShipStatus(driverMembershipEntity.getOpenId());
            }
            int result = notfirstOpenDriver(driverMemberShipRequest, driverMembershipEntity, type);
            log.info("司机开通会员(已是会员),更新影响行数：{}", result);
            return result;
        }
    }

    private void driverGift(DriverMembershipEntity driverMembershipEntity) {
        //openid_user表，司机送10次免技术服务费次数
        int resNormalMemberShipStatus = openidUserMapper.updateDriverNormalMemberShipStatus(driverMembershipEntity.getOpenId());
        int times = openidUserMapper.selectFreetime(driverMembershipEntity.getOpenId());
        //shipper_info表，司机送10次免技术服务费次数，，三个表次数保持统一
        int resShipperInfoNormalMemberShipStatus = driverInfoEntityMapper.updateNormalMemberShipStatus(driverMembershipEntity.getOpenId(), times);
        //shipper_membership表，司机送10次免技术服务费次数
        int resShipperMembershipNormalMemberShipStatus = driverMembershipEntityMapper.updateNormalMemberShipStatus(driverMembershipEntity.getOpenId(), times);
    }

    //司机非首次开通
    private int notfirstOpenDriver(DriverMemberShipRequest driverMemberShipRequest, DriverMembershipEntity
            driverMembershipEntity, String type) {
        //非首次开通，做更新操作
        int sqltype = driverMembershipEntityMapper.selectDriverMemberShipType(driverMemberShipRequest.getOpenId());
        //int转二进制字符
        String binarytype = "";
        while (sqltype != 0) {
            binarytype = sqltype % 2 + binarytype;
            sqltype = sqltype / 2;
        }
        //二进制字符串"或"运算
        String lastBinaryType = "";
        if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverRenewEnum.NORMAL.getCode())) {
            lastBinaryType = orCompute(binarytype, DriverRenewEnum.NORMAL.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverRenewEnum.OIL.getCode())) {
            lastBinaryType = orCompute(binarytype, DriverRenewEnum.OIL.getMsg());
        } else if (Objects.equals(driverMembershipEntity.getMembershipType(), DriverRenewEnum.COLD.getCode())) {
            lastBinaryType = orCompute(binarytype, DriverRenewEnum.COLD.getMsg());
        }
        int lasttype = -1;
        if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.FIRST.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.FIRST.getCode();
        } else if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.SECOND.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.SECOND.getCode();
        } else if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.THIRD.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.THIRD.getCode();
        } else if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.FOUTH.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.FOUTH.getCode();
        } else if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.FIFTH.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.FIFTH.getCode();
        } else if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.SIXTH.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.SIXTH.getCode();
        } else if (Objects.equals(lastBinaryType, DriverMemberShipTypeEnums.SEVENTH.getMsg())) {
            lasttype = DriverMemberShipTypeEnums.SEVENTH.getCode();
        }
        driverMembershipEntity.setMembershipType(lasttype);
        if (Objects.equals(type, "normal")) {
            return driverMembershipEntityMapper.updateDriverNormalMember(driverMembershipEntity);
        } else if (Objects.equals(type, "oil")) {
            return driverMembershipEntityMapper.updateDriverOilMember(driverMembershipEntity);
        } else {
            return driverMembershipEntityMapper.updateDriverColdMember(driverMembershipEntity);
        }

    }

    //货主开通会员
    private int openShipperMemberShip(ShipperMemberShipRequest shipperMemberShipRequest,
                                      int shipperMemberShipStatus, ShipperMembershipEntity shipperMembershipEntity, String type) {
        shipperMembershipEntity.setOpenId(shipperMemberShipRequest.getOpenId());
        if (Objects.equals(shipperMemberShipStatus, 0)) {
            //首次开通(之前未开通任何会员，数据库无记录)，插入
            int result = shipperMembershipEntityMapper.insertNewShipperMember(shipperMembershipEntity);
            //首次开会员，写库标记“会员”
            int res = openidUserMapper.updateMemberShipStatus(shipperMembershipEntity.getOpenId());
            if (Objects.equals(type, "normal")) {
                //货主赠送权益
                shipperGift(shipperMembershipEntity);
            } else {
                int resColdMemberShipStatus = openidUserMapper.updateColdMemberShipStatus(shipperMembershipEntity.getOpenId());
            }
            log.info("货主开通会员(之前不是任何会员),插入影响行数：{}", result);
            return result;
        } else {
            //非首次开通(之前开过某个会员，有记录,但不是以下会员)，更新即可
            if (Objects.equals(type, "normal")) {
                //货主赠送权益
                shipperGift(shipperMembershipEntity);
            } else {
                //openid_user表标记货主为冷链会员，
                int resColdMemberShipStatus = openidUserMapper.updateColdMemberShipStatus(shipperMembershipEntity.getOpenId());
            }
            int result = notfirstOpenShipper(shipperMemberShipRequest, shipperMembershipEntity, type);
            log.info("货主开通会员(已是会员),更新影响行数：{}", result);
            return result;
        }
    }

    private void shipperGift(ShipperMembershipEntity shipperMembershipEntity) {
        //openid_user表标记货主为尊享会员，并赠送398次免费发货次数,0张优惠券
        int resNormalMemberShipStatus = openidUserMapper.updateShipperNormalMemberShipStatus(shipperMembershipEntity.getOpenId());
        int times = openidUserMapper.selectFreetime(shipperMembershipEntity.getOpenId());
        //shipper_info表货主赠送398次免费发货次数,0张优惠券
        int resShipperInfoNormalMemberShipStatus = shipperInfoEntityMapper.updateNormalMemberShipStatus(shipperMembershipEntity.getOpenId(), times);
        //shipper_membership表货主赠送398次免费发货次数
        int resShipperMembershipNormalMemberShipStatus = shipperMembershipEntityMapper.updateNormalMemberShipStatus(shipperMembershipEntity.getOpenId(), times);
    }

    //货主非首次开通(之前开过某个会员，有记录)，更新即可
    private int notfirstOpenShipper(ShipperMemberShipRequest shipperMemberShipRequest, ShipperMembershipEntity
            shipperMembershipEntity, String type) {
        //非首次开通，做更新操作
        int sqltype = shipperMembershipEntityMapper.selectShipperMemberShipType(shipperMemberShipRequest.getOpenId());
        //int转二进制字符
        String binarytype = "";
        while (sqltype != 0) {
            binarytype = sqltype % 2 + binarytype;
            sqltype = sqltype / 2;
        }
        //二进制字符串"或"运算
        String lastBinaryType = "";
        if (Objects.equals(shipperMembershipEntity.getMembershipType(), ShipperRenewEnum.NORMAL.getCode())) {
            lastBinaryType = orCompute(binarytype, ShipperRenewEnum.NORMAL.getMsg());
        } else if (Objects.equals(shipperMembershipEntity.getMembershipType(), ShipperRenewEnum.COLD.getCode())) {
            lastBinaryType = orCompute(binarytype, ShipperRenewEnum.COLD.getMsg());
        }
        int lasttype = -1;
        if (Objects.equals(lastBinaryType, ShipperMemberShipTypeEnums.FIRST.getMsg())) {
            lasttype = ShipperMemberShipTypeEnums.FIRST.getCode();
        } else if (Objects.equals(lastBinaryType, ShipperMemberShipTypeEnums.SECOND.getMsg())) {
            lasttype = ShipperMemberShipTypeEnums.SECOND.getCode();
        } else if (Objects.equals(lastBinaryType, ShipperMemberShipTypeEnums.THIRD.getMsg())) {
            lasttype = ShipperMemberShipTypeEnums.THIRD.getCode();
        }
        shipperMembershipEntity.setMembershipType(lasttype);
        if (Objects.equals(type, "normal")) {
            return shipperMembershipEntityMapper.updateShipperNormalMember(shipperMembershipEntity);
        } else {
            return shipperMembershipEntityMapper.updateShipperColdMember(shipperMembershipEntity);
        }

    }

    /**
     * 当前时间加n个月
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }


    /**
     * 二进制字符串‘或’运算
     */
    public static String orCompute(String a, String b) {
        int n = Math.max(a.length(), b.length());
        StringBuffer str = new StringBuffer();
        int current = 0;
        for (int i = 0; i < n; i++) {
            current |= i < a.length() ? a.charAt(a.length() - 1 - i) - '0' : 0;
            current |= i < b.length() ? b.charAt(b.length() - 1 - i) - '0' : 0;
            str.append(current % 2);
            current /= 2;
        }
        if (current > 0) {
            str.append(1);
        }
        return new String(str.reverse());
    }

}
